package com.recruit.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.recruit.common.common.mybatis.Page;
import com.recruit.common.common.util.PageUtil;
import com.recruit.common.dto.logo.CreateOrUpdateLogoDTO;
import com.recruit.common.model.LogoDO;
import com.recruit.common.service.LogoService;
import com.recruit.common.vo.CreatedVO;
import com.recruit.common.vo.DeletedVO;
import com.recruit.common.vo.PageResponseVO;
import com.recruit.common.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;


/**
* @author Eval
* @since 2022-04-04
*/
@RestController
@RequestMapping("/recruit/logo")
@PermissionModule(value = "系统内容管理")
public class LogoController {

    @Autowired
    private LogoService logoService;

    /**
     * 新增LOGO
     * @param validator
     * @return
     */
    @Logger(template = "更新LOGO")
    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "更新LOGO")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateLogoDTO validator) {
        QueryWrapper<LogoDO> wrapper=new QueryWrapper<LogoDO>();
        wrapper.eq("no","222");
        LogoDO logoDO = logoService.getOne(wrapper);
        if(logoDO != null) {
            logoService.updateLogo(logoDO, validator);
        } else {
            logoService.createLogo(validator);
        }
        return new CreatedVO(210);
    }

    /**
     * 更新LOGO信息
     * @param id
     * @return
     */
    @Logger(template = "更新LOGO")
    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "更新LOGO")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateLogoDTO validator) {
        LogoDO logoDO = logoService.getById(id);
        if (logoDO == null) {
            throw new NotFoundException(110000);
        }
        logoService.updateLogo(logoDO, validator);
        return new UpdatedVO(210);
    }

    /**
     * 根据id删除LOGO
     * @param id
     * @return
     */
    @Logger(template = "删除LOGO")
    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除LOGO")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        LogoDO logoDO  = logoService.getById(id);
        if (logoDO == null) {
            throw new NotFoundException(110000);
        }
        logoService.deleteById(logoDO.getId());
        return new DeletedVO(220);
    }

    /**
     * 根据id查询LOGO信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public LogoDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        LogoDO logoDO = logoService.getById(id);
        if (logoDO == null) {
            throw new NotFoundException(230);
        }
        return logoDO;
    }

    /**
     * 查询所有LOGO
     * @return
     */
    @GetMapping("")
    public List<LogoDO> getAll() {
        List<LogoDO> logoDOS = logoService.getAll();
        if (logoDOS == null || logoDOS.size() == 0) {
            throw new NotFoundException(240);
        }
        return logoDOS;
    }

    /**
     * 分页查询所有LOGO列表
     * @param count
     * @param page
     * @return
     */
    @Logger(template = "查看LOGO列表")
    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "查看LOGO列表")
    public PageResponseVO<LogoDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<LogoDO> pager = new Page<>( page,count);
        IPage<LogoDO> paging = logoService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

    /**
     * 查询部分LOGO用于首页展示
     * @return
     */
    @GetMapping("/show")
    public List<LogoDO> show() {
        List<LogoDO> logoDOS = logoService.show();
        if (logoDOS == null || logoDOS.size() == 0) {
            throw new NotFoundException(240);
        }
        return logoDOS;
    }
}
